package main

import (
	"fmt"
	expect "github.com/google/goexpect"
	"github.com/google/goterm/term"
	"os"
	"regexp"
	"strings"
	"time"
)

func run(i int) {
	fmt.Println(term.Bluef(fmt.Sprintf("Retry time: %d", i)))

	e, _, err := expect.Spawn(fmt.Sprintf("/root/kk create cluster -f kk-cluster.yaml"), -1)
	if err != nil {
		fmt.Println(err)
	}

	for {
		b := true
		if !b {
			fmt.Println("break")
			break
		}
		msg, match, _ := e.Expect(regexp.MustCompile("yes/no.. |KKZONE=cn"), time.Second*5)

		fmt.Println(msg)
		fmt.Println(match)
		if len(match) == 0 {
			break
		}
		if strings.Contains(match[0], "yes") {
			fmt.Println("send")
			e.Send("yes\n")
		}
		//switch match[0] {
		//case "[yes/no]: ":
		//
		//case "~# ":
		//	b = false
		//	break
		//}
	}
}

func main() {
	//passwords := []string{"111", "222", "123"}
	//for _, p := range passwords {
	//	fmt.Println(term.Bluef(fmt.Sprintf("password: %s", p)))
	//	run(p)
	//}
	os.Chdir("/root")
	for i := range 5 {
		run(i + 1)
	}
}
